package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

//    public static  final String PAYMENT_URL="http://localhost:8001";
    public static  final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE"; //微服务的名称

    @Resource
private RestTemplate restTemplate;


@GetMapping("/consumer/payment/creat")
    public CommonResult<Payment> creat(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payments",payment,CommonResult.class);
    }


    @GetMapping(value = "/consumer/payment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id){
        log.info("请求地址"+PAYMENT_URL+"/payment/"+id);
    return   restTemplate.getForObject(PAYMENT_URL+"/payment/"+id,CommonResult.class);
    }

    @GetMapping("/consumer/payment/Object/{id}")
    public CommonResult<Payment> getPaymentObject(@PathVariable("id") long id){
    return restTemplate.getForObject(PAYMENT_URL+"/payment/"+id,CommonResult.class);
    }
    @GetMapping("/consumer/payment/Entity/{id}")
    public CommonResult<Payment> getPaymentEntity(@PathVariable("id") long id){

        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/" + id, CommonResult.class);

        if (entity.getStatusCode().is2xxSuccessful()){
            log.info(entity.toString());
            return entity.getBody();
        }else{
            return new CommonResult<>(444,"操作失败");
        }


    }





}
